﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class FND_CASHDISBURSEMENT_POSTED : RibbonForm
    {
        public FND_CASHDISBURSEMENT_POSTED()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedDocumentNumber { get; set; }
        public string DocumentType { get; set; }

        private void SearchByDocument()
        {
            CashDisbursementManager oManager = new CashDisbursementManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.BrowsePostedDisbursementByDocument(txtSearch.Text);
            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        private void SearchByRemarks()
        {
            CashDisbursementManager oManager = new CashDisbursementManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.BrowsePostedDisbursementByRemarks(txtSearch.Text);
            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        private void SearchByAccountOrCustomer()
        {
            CashDisbursementManager oManager = new CashDisbursementManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.BrowsePostedDisbursementByCardName(txtSearch.Text);
            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }


        private void SearchByReceiptNumber()
        {
            CashDisbursementManager oManager = new CashDisbursementManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dt = oManager.BrowsePostedDisbursementByReceiptNumBer(this.DocumentType, txtSearch.Text);
            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }



        private void FND_CASHRECEIPT_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;
            
            this.SelectedDocumentNumber = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["DocNum"]).ToString();

            this.Close();
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (cboSearch.SelectedIndex)
            {
                case 0:
                    SearchByDocument();
                    break;
                case 1:
                    SearchByAccountOrCustomer();
                    break;
                case 2:
                    SearchByReceiptNumber();
                    break;
                case 3:
                    SearchByRemarks();
                    break;
            }
            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                CashDisbursementManager oManager = new CashDisbursementManager();
                DataTable dt = new DataTable();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                dt = oManager.BrowseDisbursementByDocument(txtSearch.Text);
                oManager.Close();

                gridControl1.DataSource = dt;
                gridControl1.Refresh();
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
